package kr.couponplay.android.activity.deal.category;

import kr.couponplay.android.Category;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.deal.DealListActivity;
import kr.couponplay.android.activity.deal.menu.SubMenuHelper;
import kr.couponplay.android.activity.deal.menu.TopMenuHelper;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class CategoryListFragment extends ListFragment {
	private DealListActivity activity;
	private CategoryAdapter categoryAdapter;
	
	public static CategoryListFragment newInstance(DealListActivity activity) {
		CategoryListFragment fragment = new CategoryListFragment();
		fragment.activity = activity;
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.category_list, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		categoryAdapter = new CategoryAdapter(getActivity());
		CategoryHelper.draw(categoryAdapter, TopMenuHelper.selectedTopMenuButtonIndex);
		setListAdapter(categoryAdapter);
	}

	public CategoryAdapter getCategoryAdapter() {
		return categoryAdapter;
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
		Category category = categoryAdapter.getItem(position);
		
		if (category.getLevelNo() == 1) {
			CategoryHelper.mainCategoryId = category.getId();
			CategoryHelper.subCategoryId = 0;
		} else {
			CategoryHelper.mainCategoryId = category.getParentId();
			CategoryHelper.subCategoryId = category.getId();
		}
		
		SubMenuHelper.selectSubMenuButton(0);
		SubMenuHelper.setCategorySubMenuText(category.getName());
		activity.toggle();
		activity.getDealList();
    }
}
